<?php

namespace App\Admin\Controllers;

use App\Models\Department;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Show;
use Illuminate\Support\MessageBag;

class DepartmentController extends Controller
{
    use ModelForm;

    public $header = '部门列表';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Department::class, function (Grid $grid) {

            $grid->id('编号')->sortable();
            $grid->name('名称')->editable();
            $grid->description('职能描述');

            $grid->users('成员数量')->display(function ($v){
                return count($v);
            });
            $grid->created_at('添加时间');
            $states = [
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ];
            $grid->status('是否启用')->switch($states);

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->like('name', '名称');
                $filter->between('created_at', '添加时间')->datetime();
            });

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableView();
                /*if (count($actions->row->users) > 0){
                    $actions->disableDelete();
                }*/
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Department::class, function (Form $form) {

            $form->display('id', '编号');
            $form->text('name', '名称');
            $form->textarea('description', '职能描述');
            $states = [
                'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
                'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
            ];
            $form->switch('status', '是否启用')->states($states)->default('1');
            $form->display('created_at', '创建时间');

            //$form->listbox('permissions')->options(Department\Permission::all()->pluck('name', 'id'))->settings(['selectorMinimalHeight' => 300]);

            $form->disableViewCheck();

        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->header)
            ->description('Detail')
            ->body($this->detail($id));
    }

    public function detail($id)
    {
        $show = new Show(Department::findOrFail($id));

        $show->name('名称');
        $show->description('职能描述');

        return $show;
    }

    public function destroy($id)
    {
        $user_count = Department\User::where('department_id', $id)->count();
        if ($user_count) {
            admin_toastr('删除失败,该部门下有成员存在,不能直接删除', 'error');
        } else {
            $status = Department::where('id', $id)->delete();

            if ($status){
                admin_toastr('删除成功', 'success');
            } else {
                admin_toastr('删除失败', 'error');
            }
        }

    }

}
